package com.ysd.lis.controller.sys;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ysd.common.Result;
import com.ysd.common.dto.DeletesDto;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.dto.EditInterfaceDictParam;
import com.ysd.lis.entity.SysInterfaceDict;
import com.ysd.lis.entity.SysInterfaceDictField;
import com.ysd.lis.entity.SysInterfaceDictParam;
import com.ysd.lis.service.*;
import com.ysd.util.ToolsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@Api(tags = "接口管理")
@RestController
public class SysInterfaceDictController extends BaseController {

    @Autowired
    SysInterfaceDictService sysInterfaceDictService;

    @Autowired
    SysInterfaceDictParamService sysInterfaceDictParamService;

    @Autowired
    SysInterfaceDictFieldService sysInterfaceDictFieldService;

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;



    @ApiOperation("获取接口信息（不分页)")
    @GetMapping("/api/sys/getInterfaceDictList")
    public Result getIntfList( SysInterfaceDict sysInterfaceDict) {
        LambdaQueryWrapper<SysInterfaceDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (ToolsUtils.isNotEmpty(sysInterfaceDict.getKeyWords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SysInterfaceDict::getName, sysInterfaceDict.getKeyWords()).or().like(SysInterfaceDict::getCode, sysInterfaceDict.getKeyWords()));
        }

        lambdaQueryWrapper.like(StrUtil.isNotEmpty(sysInterfaceDict.getOwnSystem()),SysInterfaceDict::getOwnSystem,sysInterfaceDict.getOwnSystem());
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(sysInterfaceDict.getInterfaceGroup()),SysInterfaceDict::getInterfaceGroup,sysInterfaceDict.getInterfaceGroup());
        lambdaQueryWrapper.like(SysInterfaceDict::getDelFlag,0);
        lambdaQueryWrapper.orderByAsc(SysInterfaceDict::getTypeName);
        lambdaQueryWrapper.orderByAsc(SysInterfaceDict::getOrderCode);
        List<SysInterfaceDict> sysInterfaceDicts = sysInterfaceDictService.getBaseMapper().selectList(lambdaQueryWrapper);

        return Result.succ(sysInterfaceDicts);
    }

    @ApiOperation("获取接口信息（不分页)")
    @GetMapping("/api/sys/getInterfaceDictParamList")
    public Result getIntfParamList( String dictId,String type) {
        LambdaQueryWrapper<SysInterfaceDictParam> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterfaceDictParam::getDictId,dictId).eq(SysInterfaceDictParam::getType,type).orderByAsc(SysInterfaceDictParam::getSeq);
        return Result.succ(sysInterfaceDictParamService.list(lambdaQueryWrapper));
    }


    @ApiOperation("修改接口")
    @PostMapping("/api/sys/editInterfaceDict")
    @LogDetail(requestParam ="editIntf",operationType = OperationType.UPDATE, module = "接口管理",content = "修改接口操作")
    public Result editInterfaceDict(@RequestBody SysInterfaceDict sysInterfaceDict) {
        return  Result.succ(sysInterfaceDictService.getBaseMapper().updateById(sysInterfaceDict));
    }

    @ApiOperation("新增接口")
    @PostMapping("/api/sys/addInterfaceDict")
    @LogDetail(requestParam ="editIntf",operationType = OperationType.INSERT, module = "接口管理",content = "新增接口操作")
    public Result addInterfaceDict(@RequestBody SysInterfaceDict sysInterfaceDict) {
        return  Result.succ(sysInterfaceDictService.getBaseMapper().insert(sysInterfaceDict));
    }

    @ApiOperation("编辑接口参数")
    @PostMapping("/api/sys/editInterfaceDictParam")
    @LogDetail(requestParam ="editIntf",operationType = OperationType.UPDATE, module = "接口管理",content = "修改接口参数操作")
    public Result editInterfaceDictParam(@RequestBody EditInterfaceDictParam editInterfaceDictParam) {
        if(CollectionUtils.isNotEmpty(editInterfaceDictParam.getList())){
            sysInterfaceDictParamService.saveOrUpdateBatch(editInterfaceDictParam.getList());
        }
        return  Result.succ("编辑成功！");
    }

    @ApiOperation("删除接口参数")
    @PostMapping("/api/sys/deleteInterfaceDictParam")
    @LogDetail(requestParam ="deleteInterfaceDictParam",operationType = OperationType.DELETE, module = "删除接口参数",content = "删除接口参数")
    public Result deleteInterfaceDictParam(@RequestBody DeletesDto deletesDto) {
        sysInterfaceDictParamService.removeByIds(deletesDto.getIds());
        return  Result.succ("删除成功！");
    }



    @ApiOperation("删除接口参数")
    @PostMapping("/api/sys/deleteInterfaceDict")
    @LogDetail(requestParam ="deleteInterfaceDict",operationType = OperationType.DELETE, module = "删除接口参数",content = "删除接口参数")
    public Result deleteInterfaceDict(@RequestBody DeletesDto deletesDto) {
        sysInterfaceDictService.removeByIds(deletesDto.getIds());
        LambdaQueryWrapper<SysInterfaceDictParam> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysInterfaceDictParam::getDictId,deletesDto.getIds());
        sysInterfaceDictParamService.remove(lambdaQueryWrapper);

        LambdaQueryWrapper<SysInterfaceDictField> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(SysInterfaceDictField::getPid,deletesDto.getIds());
        sysInterfaceDictFieldService.remove(lambdaQueryWrapper1);

        return Result.succ("删除成功");
    }


}
